package edu.faytechcc.csc.gui;

import edu.faytechcc.csc.database.LogIntoDatabase;
import edu.faytechcc.csc.messages.Connection;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * Please describe what this class does here.
 *
 * CSC 251 0001 DATE:
 *
 * @author Stephanie Mckenzie
 */
public class ChatWindow
{

    private Connection conn;
    private JTextArea messagesArea;
    private JTextField messageTextField; //to enter a message to send
    private JButton sendButton;
    private String localUserID;

    public ChatWindow(Connection conn, String name)
    {
        this.conn = conn;
        this.localUserID = name;
    }

    public void createAndShowGUI()
    {
        JFrame frame = new JFrame("Chat Client (" + this.localUserID + ")");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        addComponentsToPane(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
    }

    private void addComponentsToPane(Container pane)
    {
        pane.setLayout(new BorderLayout());

        this.messagesArea = new JTextArea(10, 50);
        JScrollPane scrollPane = new JScrollPane(this.messagesArea);
        this.messagesArea.setEditable(false);
        pane.add(scrollPane, BorderLayout.CENTER);

        this.messageTextField = new JTextField(50);
        pane.add(this.messageTextField, BorderLayout.SOUTH);
        this.messageTextField.addActionListener(new TextFieldActionListener());
        this.messageTextField.setEditable(false);
    }
    
    public void enableTextField()
    {
        this.messageTextField.setEditable(true);
    }

    public void displayMessage(String remoteUserID, String msg)
    {
        if (msg.startsWith("~~LogIn:"))
        {
            String[] namePass = msg.split(",");
            String name = namePass[1];
            String password = namePass[2];
            char[] pass = password.toCharArray();
            
            LogIntoDatabase logIn = new LogIntoDatabase(name, pass);
            
            msg = ("UserName: " + name
                    + "\nPassword: " + password + "\n"
                    + logIn.toString());
            
            conn.sendMessages(logIn.toString());
        }
        messagesArea.append(remoteUserID + ": " + msg + "\n");
        messagesArea.setCaretPosition(messagesArea.getDocument().getLength());
    }

    /**
     * @return the localUserID
     */
    public String getLocalUserID()
    {
        return localUserID;
    }

    class TextFieldActionListener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            String msg = messageTextField.getText();
            conn.sendMessages(msg);

            messagesArea.append(msg + "\n");
            messageTextField.setText("");
            messagesArea.setCaretPosition(messagesArea.getDocument().getLength());
        }
    }
}
